﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

//矩阵中战斗力最弱的K行
//https://leetcode.cn/problems/the-k-weakest-rows-in-a-matrix/submissions/
class Solution {
public:
    vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
        map<int, vector<int>> m;

        vector<int> ret;
        ret.reserve(k + 1);

        int n = mat.size();
        for (int i = 0; i < n; ++i)
        {
            int num = 0;
            for (const auto& x : mat[i])
            {
                if (x == 1) ++num;
                else break;
            }
            m[num].push_back(i);
        }

        int count = k;
        for (const auto& x : m)
            for (const auto& pos : x.second)
            {
                ret.push_back(pos);
                if (!--count) return ret;
            }


        return ret;
    }
};

//分割平衡字符串
//https://leetcode.cn/problems/split-a-string-in-balanced-strings/submissions/
class Solution {
public:
    int balancedStringSplit(string s) {
        int count = 0;
        int num = 0;
        for (const auto& c : s)
        {
            num += c == 'R' ? 1 : -1;
            if (num == 0) ++count;
        }
        return count;
    }
};